package com.have.blog.xo.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.commons.feign.PictureFeignClient;
import com.have.blog.model.api.CommonPage;
import com.have.blog.model.mapper.TPictureMapper;
import com.have.blog.model.mapper.TPictureSortMapper;
import com.have.blog.model.models.TPictureExample;
import com.have.blog.model.models.TPictureSort;
import com.have.blog.model.models.TPictureSortExample;
import com.have.blog.utils.MyStringUtils;
import com.have.blog.utils.ResultUtils;
import com.have.blog.xo.constant.MessageConstant;
import com.have.blog.xo.constant.SysConstant;
import com.have.blog.xo.service.admin.AdminPictureSortService;
import com.have.blog.xo.uitls.WebUtil;
import com.have.blog.xo.vo.AdminPictureSortVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
@SuppressWarnings("all")
@Slf4j
public class AdminPictureSortServiceImpl implements AdminPictureSortService {
    @Autowired
    private TPictureSortMapper pictureSortMapper;
    @Autowired
    private TPictureMapper pictureMapper;
    @Autowired
    private PictureFeignClient pictureFeignClient;
    @Autowired
    private WebUtil webUtil;

    @Override
    public String getList(AdminPictureSortVo pictureSortVO) {
        PageHelper.startPage(pictureSortVO.getCurrentPage(), pictureSortVO.getPageSize());
        TPictureSortExample example = new TPictureSortExample();
        TPictureSortExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(pictureSortVO.getKeyword()) && !StringUtils.isEmpty(pictureSortVO.getKeyword().trim())) {
            criteria.andNameLike("%" + pictureSortVO.getKeyword() + "%");
        }
        if (pictureSortVO.getIsShow() != null) {
            criteria.andIsShowEqualTo(pictureSortVO.getIsShow());
        }
        criteria.andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        example.setOrderByClause("sort desc");
        List<TPictureSort> list = pictureSortMapper.selectByExample(example);

        StringBuilder sb = new StringBuilder();
        list.forEach(item -> {
            if (!StringUtils.isEmpty(item.getFileUid())) {
                sb.append(item.getFileUid() + SysConstant.FILE_SEGMENTATION);
            }
        });
        String pictureResult = null;
        HashMap<String, String> pictureMap = new HashMap<>();
        if (!StringUtils.isEmpty(sb.toString())) {
            // TODO  图片信息服务接口获取图片信息
            pictureResult = pictureFeignClient.getPicture(sb.toString(), SysConstant.FILE_SEGMENTATION);
        }
        List<Map<String, Object>> picList = webUtil.getPictureMap(pictureResult);
        picList.forEach(item -> {
            pictureMap.put(item.get(SysConstant.UID).toString(), item.get(SysConstant.URL).toString());
        });
        // TODO  设置图片结果
        for (TPictureSort item : list) {
            if (!StringUtils.isEmpty(item.getFileUid())) {
                List<String> pictureUidList = MyStringUtils.changeStringToStringList(item.getFileUid(), SysConstant.FILE_SEGMENTATION);
                List<String> tempPictureList = new ArrayList<>();
                pictureUidList.forEach(picture -> {
                    tempPictureList.add(pictureMap.get(picture));
                });
                item.setPictureList(tempPictureList);
            }
        }
        return ResultUtils.successWithData(CommonPage.restPage(list));
    }

    @Override
    public String addPictureSort(AdminPictureSortVo pictureSortVO) {
        TPictureSortExample example = new TPictureSortExample();
        TPictureSortExample.Criteria criteria = example.createCriteria()
                .andNameEqualTo(pictureSortVO.getName());
        if (pictureSortVO.getParentUid() != null) {
            criteria.andParentUidEqualTo(pictureSortVO.getParentUid());
        }
        List<TPictureSort> pictureSortList = pictureSortMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(pictureSortList)) {
            return ResultUtils.errorWithMessage(MessageConstant.ENTITY_EXIST);
        }
        TPictureSort tPictureSort = new TPictureSort();
        tPictureSort.setUid(MyStringUtils.getUUID());
        tPictureSort.setName(pictureSortVO.getName());
        tPictureSort.setUid(MyStringUtils.getUUID());
        tPictureSort.setParentUid(pictureSortVO.getParentUid());
        tPictureSort.setSort(pictureSortVO.getSort());
        tPictureSort.setFileUid(pictureSortVO.getFileUid());
        tPictureSort.setIsShow(pictureSortVO.getIsShow());
        tPictureSort.setCreateTime(new Date());
        tPictureSort.setUpdateTime(new Date());
        pictureSortMapper.insertSelective(tPictureSort);
        return ResultUtils.successWithData(MessageConstant.INSERT_SUCCESS);
    }

    @Override
    public String editPictureSort(AdminPictureSortVo pictureSortVO) {
        TPictureSort pictureSort = pictureSortMapper.selectByPrimaryKey(pictureSortVO.getUid());
        if (pictureSort == null) {
            return ResultUtils.errorWithMessage(MessageConstant.ENTITY_NOT_EXIST);
        }
        pictureSort.setName(pictureSortVO.getName());
        pictureSort.setParentUid(pictureSortVO.getParentUid());
        pictureSort.setSort(pictureSortVO.getSort());
        pictureSort.setFileUid(pictureSortVO.getFileUid());
        pictureSort.setIsShow(pictureSortVO.getIsShow());
        pictureSort.setUpdateTime(new Date());
        pictureSortMapper.updateByPrimaryKeySelective(pictureSort);
        return ResultUtils.successWithMessage(MessageConstant.UPDATE_SUCCESS);
    }

    @Override
    public String deletePictureSort(AdminPictureSortVo pictureSortVO) {
        TPictureExample pictureExample = new TPictureExample();
        pictureExample.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andPictureSortUidEqualTo(pictureSortVO.getUid());
        long count = pictureMapper.countByExample(pictureExample);
        if (count > 0) {
            return ResultUtils.errorWithMessage(MessageConstant.PICTURE_UNDER_THIS_SORT);
        }
        TPictureSort pictureSort = pictureSortMapper.selectByPrimaryKey(pictureSortVO.getUid());
        pictureSort.setStatus(BlogStatus.DISABLE.getStatus());
        pictureSort.setUpdateTime(new Date());
        pictureSortMapper.updateByPrimaryKeySelective(pictureSort);
        return ResultUtils.successWithMessage(MessageConstant.DELETE_SUCCESS);
    }

    @Override
    public String stickPictureSort(AdminPictureSortVo pictureSortVO) {
        TPictureSort pictureSort = pictureSortMapper.selectByPrimaryKey(pictureSortVO.getUid());
        PageHelper.startPage(1, 1);
        TPictureSortExample example = new TPictureSortExample();
        example.setOrderByClause("sort desc");
        List<TPictureSort> pictureSortList = pictureSortMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(pictureSortList)) {
            return ResultUtils.successWithMessage(MessageConstant.OPERATION_SUCCESS);
        }
        TPictureSort maxSort = pictureSortList.get(0);
        pictureSort.setSort(maxSort.getSort() + 1);
        pictureSort.setUpdateTime(new Date());
        pictureSortMapper.updateByPrimaryKeySelective(pictureSort);
        return ResultUtils.successWithMessage(MessageConstant.OPERATION_SUCCESS);
    }

    @Override
    public String getById(String uid) {
        TPictureSort pictureSort = pictureSortMapper.selectByPrimaryKey(uid);
        if (pictureSort == null) {
            return ResultUtils.errorWithMessage(MessageConstant.ENTITY_NOT_EXIST);
        }
        return ResultUtils.successWithData(pictureSort);
    }
}
